<?php
/**
 * created by zzy
 * date: 2018/1/16 10:48
 */
namespace App\Eloquent\Ygt;

class Qrcode extends DbEloquent{

    protected $table    = 'ygt_qrcode';

    //生成二维码的编号
    public static function getSn($productNo)
    {
        $sn                 = $productNo.$_SERVER['REQUEST_TIME'].mt_rand(10000,99999);
        return $sn;
    }
    //解析二维码的内容
    //$isStrict true严格验证
    public static function decodeStr($qrcodeStr='',$isStrict=true)
    {
        $result             = false;

        if(!strstr($qrcodeStr,'-')){
            $where          = ['sn'=>$qrcodeStr];
            $info           = self::getInfo($where);
            $qrcodeStr = "3-".$info['id']."-".$qrcodeStr;
        }

        $qrcodeArr          = explode('-',$qrcodeStr);
        $size               = count($qrcodeArr);
        if($size == 3)
        {
            $from           = intval($qrcodeArr[0]);
            $qrcodeId       = intval($qrcodeArr[1]);
            $qrcodeSn       = $qrcodeArr[2];
            $result         = [
                'from'=>$from,
                'id'=>$qrcodeId,
                'sn'=>$qrcodeSn,
            ];
            if($isStrict)
            {
                $where          = ['id'=>$qrcodeId];
                $info           = self::getInfo($where);
                if(!$info || $info->sn!=$qrcodeSn)
                {
                    $result     = false;
                }
            }
        }
        return $result;
    }
    //获取二维的的类型
    //$qrcode 数据库中取出来的数据
    //返回值
    //1绑定了材料且入了库
    //2绑定了材料未入库
    //3没有绑定材料的
    public static function getQrcodeType($qrcode)
    {
        $stockId            = $qrcode->stock_id;
        $productId          = $qrcode->product_id;
        $qrcodeType         = 3;
        if($productId)
        {
            $qrcodeType     = 1;
            if(!$stockId)
            {
                $qrcodeType = 2;
            }
        }
        return $qrcodeType;
    }

}
